package com.yulebaby.callsenter.global;

import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.provider.CallLog;

import com.facebook.stetho.Stetho;
import com.orhanobut.logger.Logger;
import com.yulebaby.callsenter.http.HttpHelper;
import com.yulebaby.callsenter.observer.CallLogObserver;
import com.yulebaby.callsenter.utils.SPUtil;

import java.io.File;

import cn.jpush.android.api.JPushInterface;

/**
 * Author: Zhangbo
 * Time: 2016年11月8日
 */

public class App extends Application {

    public static final String FILE_BASE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "YuLeBaBy";


    public static Context mContext;
    public static Handler mHandler;
    private CallLogObserver callLogObserver;

    public static volatile boolean isFinish = false;
    public static volatile boolean mCallFlag = false;
    public static volatile boolean isState = false;

    public final static boolean DEBUG = true;//TODO 在DEBUG模式，log会显示，发布时注意设置为false

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        //观察者使用的handler
        mHandler = new Handler();
        //初始化Log打印
        Logger.init("yulebaby");
        SPUtil.init(mContext, "yulebaby");
        //调试

        Stetho.initializeWithDefaults(this);
        //拦截崩溃事件
//        registerCrashHandler();
        //极光推送
        JPushInterface.setDebugMode(false);    // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);            // 初始化 JPush
        initHttpHelper();
        initObserver();

    }

    /**
     * 初始化通话记录的Observer
     */
    private void initObserver() {
        callLogObserver = new CallLogObserver();
        getContentResolver().registerContentObserver(CallLog.Calls.CONTENT_URI, true, callLogObserver);
    }

    private void initHttpHelper() {
        HttpHelper.init(this);
    }

    private void registerCrashHandler() {
        CrashHandler crash = CrashHandler.getInstance();
        crash.init(this);
        Thread.setDefaultUncaughtExceptionHandler(crash);
    }

    public static Handler getMainHandler() {
        return mHandler;
    }

}
